<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jq-3.6.0.js"></script>
</head>

<body>
    <div>书名:<input id="name" /></div>
    <div>作者:<input id="author" /></div>
    <div>价格:<input type="number" id="price" /></div>
    <div><button onclick="submit()">修改</button></div>
</body>
<script>
    const getId = () => {
        return window.location.href.split('?')[1].split("=")[1];
    }

    const getById = () => {
        let id = getId();
        // get 一般都是用来获取一个值 by 通过什么什么来获取
        //query、list、select 查询的意思 都是一个列表
        //add、updateById
        $.get('http://localhost:8082/books/getById', {
            id: id
        }, res => {
            console.log(res[0]);
            $('#name').val(res[0].name)
            $('#author').val(res[0].author)
            $('#price').val(res[0].price)
        })
    }
    getById()


    const submit = () => {
        let id = getId();
        let name = $("#name").val();
        let author = $("#author").val();
        let price = $("#price").val();

        $.post('http://localhost:8082/books/updateById', {
            id: id,
            name: name,
            author: author,
            price: price,
        }, res => {
            console.log(res);
            if (res.affectedRows == 1) {
                window.location.href = 'index.html'
            } else {
                alert('修改失败')
            }
        })

    }
</script>

</html>